package com.woniuxy.backup;

import com.rabbitmq.client.*;
import com.woniuxy.utils.Utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class NormalConsume1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //声明一个连接对象
        Connection connection = Utils.getConnection();
        //创建一个对象
        Channel channel = connection.createChannel();
        //绑定一个备用交换机  固定写法
        Map<String,Object> map=new HashMap<>();
        map.put("alternate-exchange","backExchange");
        //声明一个目标交换机 绑定一个备用交换机
        channel.exchangeDeclare("normalExchange",BuiltinExchangeType.DIRECT,false,false,map);
        //声明队列
        channel.queueDeclare("normalQueue",false,false,false,null);
        //声明队列 并绑定队列
        channel.queueBind("normalQueue","normalExchange","normal1");
        //接受消息
        channel.basicConsume("normalQueue",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("NormalConsume1 接受消息："+new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}
